function Get-TokenFile
{
	if ([io.file]::exists($XmlConfig.Root.ContentDeployment.Export.TokenFileLocation))
	{
		Import-Csv $XmlConfig.Root.ContentDeployment.Export.TokenFileLocation 
	}
	else
	{
		@()
	}
}

function Add-Token
{
Param(
  [Parameter(Mandatory=$true,Position=1)][string]$Job,
  [Parameter(Mandatory=$true,Position=2)][string]$Timestamp,
  [Parameter(Mandatory=$true,Position=3)][string]$Directory,
  [Parameter(Mandatory=$true,Position=4)][bool]$Imported,
  [Parameter(Mandatory=$true,Position=5)][string]$Mode
)
	$outTokenFile = @()
	
	$outTokenFile += new-object -typename psobject -property @{
		Job 						= 	$Job;
		Timestamp					= 	$Timestamp;
		Directory 					=	$Directory;
		Imported					=	$Imported;
		Mode						= 	$Mode;
	}
	foreach($entry in Get-TokenFile)
	{
		if ( ($entry.Job -eq "")-or ($entry.Job -eq $null)){continue;}
		
		$outTokenFile += new-object -typename psobject -property @{
			Job 						= 	$entry.Job;
			Timestamp					= 	$entry.Timestamp;
			Directory 					=	$entry.Directory;
			Imported					=	$entry.Imported;
			Mode						= 	$entry.Mode;
		}
	}
	
	$outTokenFile | Export-Csv $XmlConfig.Root.ContentDeployment.Export.TokenFileLocation -notypeinformation
}

function Change-TokenImportedStatus
{
Param(
  [Parameter(Mandatory=$true,Position=1)][string]$Timestamp,
  [Parameter(Mandatory=$true,Position=2)][bool]$Imported
)
	$outTokenFile = @()
	
	foreach($entry in Get-TokenFile)
	{
		if ( ($entry.Job -eq "")-or ($entry.Job -eq $null)){continue;}
		
		
		$outTokenFile += new-object -typename psobject -property @{
			Job 						= 	$entry.Job;
			Timestamp					= 	$entry.Timestamp;
			Directory 					=	$entry.Directory;
			Imported					=	if ($entry.Timestamp -eq $Timestamp )	{$Imported;} else{$entry.Imported;}
			Mode						= 	$entry.Mode;
		}
	}
	
	$outTokenFile | Export-Csv $XmlConfig.Root.ContentDeployment.Export.TokenFileLocation -notypeinformation
}

function Get-Token
{
Param(
  [Parameter(Mandatory=$true,Position=1)][string]$Identity
)
	Get-TokenFile |?{($_.Job -eq $Identity)}|sort Timestamp -desc|select -first 1
}

function Get-TokenForJob
{
Param(
  [Parameter(Mandatory=$true,Position=1)][string]$Job
)
	Get-TokenFile |?{(($_.Job -eq $Job)-and($_.Imported -eq $false))}|sort Timestamp -desc|select -first 1
}


